from model.user import User
from pages.add_movie_page import AddMoviePage
from pages.user_management_page import UserManagementPage
from pages.user_page_profile import UserProfilePage
from php4dvd.pages.login_page import LoginPage
from php4dvd.pages.internal_page import InternalPage
from php4dvd.pages.remove_movie_page import RemoveMoviePage
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import presence_of_element_located, alert_is_present
from selenium.webdriver.support.wait import WebDriverWait


class Application(object):

    def __init__(self, driver, base_url):
        driver.get(base_url)
        self.login_page = LoginPage(driver, base_url)
        self.internal_page = InternalPage(driver, base_url)
        self.user_profile_page = UserProfilePage(driver, base_url)
        self.user_management_page = UserManagementPage(driver, base_url)
        self.remove_movie_page = RemoveMoviePage(driver, base_url)
        self.add_movie_page = AddMoviePage(driver, base_url)
        self.wait = WebDriverWait(driver, 10);

    def go_to_home_page(self):
        self.driver.get(self.base_url)

    def login(self, user):
        lp = self.login_page
        lp.username_field.clear()
        lp.username_field.send_keys(user.username)
        lp.password_field.clear()
        lp.password_field.send_keys(user.password)
        lp.submit_button.click()

    def logout(self):
        self.internal_page.logout_button.click()
        self.wait.until(alert_is_present()).accept()

    def ensure_logout(self):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            self.logout()

    def ensure_login_as(self, user):
        element = self.wait.until(presence_of_element_located((By.CSS_SELECTOR, "nav, #loginform")))
        if element.tag_name == "nav":
            if self.is_logged_in_as(user):
                return
            else:
                self.logout()
        self.login(user)

    def is_logged_in(self):
        return self.internal_page.is_this_page

    def is_logged_in_as(self, user):
        return self.is_logged_in() \
            and self.get_logged_user().username == user.username

    def is_not_logged_in(self):
        return self.login_page.is_this_page

    def get_logged_user(self):
        self.internal_page.user_profile_link.click()
        upp = self.user_profile_page
        upp.is_this_page
        return User(username=upp.user_form.username_field.get_attribute("value"),
                    email=upp.user_form.email_field.get_attribute("value"))

    def remove_movie(self):
        rm = self.remove_movie_page
        self.wait.until(presence_of_element_located((By.CLASS_NAME, "nocover")))
        rm.div_nocover.click()
        self.wait.until(presence_of_element_located((By.CLASS_NAME, "maininfo_full")))
        rm.remove_button.click()
        self.wait.until(alert_is_present()).accept()

    def removed(self):
        try:
            self.wait.until(presence_of_element_located((By.ID, "results")))
            return True
        except WebDriverException:
            return False

    def add_movie(self):
        am = self.add_movie_page
        am.add_button.click()
        self.wait.until(presence_of_element_located((By.ID, "updateform")))
        am.imdbid_field().clear()
        am.imdbid_field().send_keys("12345")
        am.name_field().clear()
        am.name_field().send_keys("film1")
        am.aka_field().clear()
        am.aka_field().send_keys("filn1")
        am.duration_field().clear()
        am.duration_field().send_keys("90")
        am.rating_field().clear()
        am.rating_field().send_keys("12")
        am.own_no_radiobutton().click()
        am.seen_no_radiobutton().click()
        am.country_field().clear()
        am.country_field().send_keys("Russia-USA")
        am.director_field().clear()
        am.director_field().send_keys("me")
        am.writer_field().clear()
        am.writer_field().send_keys("me")
        am.year_field().clear()
        am.year_field().send_keys("2013")
        am.submit_button().click()

    def movie_is_added(self):
        try:
            self.wait.until(presence_of_element_located((By.CLASS_NAME, "maininfo_full")))
            return True
        except WebDriverException:
            return False